package com.block.merchant.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.block.account.enums.AccountTypeEnum;
import com.block.account.enums.ActionTypeEnum;
import com.block.account.model.dto.ChangeAmountDTO;
import com.block.account.model.result.ChangeAmountResult;
import com.block.account.service.IAccountHandleService;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.enums.BaseStatusEnum;
import com.block.common.vo.ResponseVo;
import com.block.db.entity.MerchantBaseInfo;
import com.block.db.entity.MerchantMealInfo;
import com.block.db.entity.MerchantMealOrderInfo;
import com.block.db.mapper.MerchantMealOrderInfoMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.merchant.enums.OrderStatusEnum;
import com.block.merchant.service.IMerchantBaseInfoService;
import com.block.merchant.service.IMerchantMealInfoService;
import com.block.merchant.service.IMerchantMealOrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.time.LocalDate;

/**
 * <p>
 * 商户套餐订单信息 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-04-19
 */
@Service
public class MerchantMealOrderInfoServiceImpl extends ServiceImpl<MerchantMealOrderInfoMapper, MerchantMealOrderInfo> implements IMerchantMealOrderInfoService {

    @Autowired
    IMerchantMealInfoService merchantMealInfoService;
    @Autowired
    IMerchantBaseInfoService merchantBaseInfoService;
    @Autowired
    IAccountHandleService accountHandleService;

    @Override
    @Transactional
    public ResponseVo createMealOrder(String infoId, String mealId, String operatorId, String operatorName){

        MerchantMealInfo mealInfo = merchantMealInfoService.getById(mealId);
        if (ObjectUtil.isEmpty(mealInfo)){
            return new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION, "套餐信息为空");
        }
        if (ObjectUtil.equals(mealInfo.getStatus(), BaseStatusEnum.ENABLE.getValue())){
            return new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION, "套餐状态不可用");
        }

        MerchantBaseInfo merchantBaseInfo = merchantBaseInfoService.getById(infoId);
        if (ObjectUtil.isEmpty(mealInfo)){
            return new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION, "商户信息为空");
        }

        MerchantMealOrderInfo mealOrderInfo = new MerchantMealOrderInfo();
        mealOrderInfo.setDateInfo(LocalDate.now().toString());
        mealOrderInfo.setInfoId(merchantBaseInfo.getId());
        mealOrderInfo.setInfoName(merchantBaseInfo.getInfoName());
        mealOrderInfo.setMealId(mealInfo.getId());
        mealOrderInfo.setMealName(mealInfo.getMealName());
        mealOrderInfo.setMealType(mealInfo.getMealType());
        mealOrderInfo.setZhiCoinAmount(mealInfo.getRealAmount());
        mealOrderInfo.setStatus(OrderStatusEnum.SUCCESS.getValue());

        boolean save = this.save(mealOrderInfo);

        // 组装 支付信息
        ChangeAmountDTO changeAmountDTO = new ChangeAmountDTO();
        changeAmountDTO.setAccountTypeEnum(AccountTypeEnum.MERCHANT);
        changeAmountDTO.setActionTypeEnum(ActionTypeEnum.BUY);
        changeAmountDTO.setChangeZhiCoinAmount(BigDecimal.ZERO.subtract(mealInfo.getRealAmount()));
        changeAmountDTO.setAccountId(merchantBaseInfo.getId());
        String dataInfo = StrUtil.format("套餐订单号：{}", mealOrderInfo.getId());
        changeAmountDTO.setDataInfo(dataInfo);
        String remarkInfo = StrUtil.format("{}[{}] 购买套餐 {}[{}]",
                merchantBaseInfo.getInfoName(), merchantBaseInfo.getId(), mealInfo.getMealName(), mealInfo.getId());
        changeAmountDTO.setRemarkInfo(remarkInfo);
        changeAmountDTO.setOperatorId(operatorId);
        changeAmountDTO.setOperatorName(operatorName);

        ChangeAmountResult changeAmountResult = accountHandleService.changeAmountHandle(changeAmountDTO);
        if (!changeAmountResult.isSuccess()){
            //
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION, changeAmountResult.getMessage());
        }
        return new ResponseVo(AppRspCodeConstant.SUCCESS);
    }

}
